<?php
namespace modules\potential;
use modules\auth\security;

class storage
{
    static function schemas(): array
    {
        return [
            'potentials' => [
                'id' => 'identity',
                'org' => '@orgs/org',
                'name' => 'string/64',
                'contact' => '@contacts/contact',
                'responsible' => '@users/user',
                'active' => 'bool',
                'comment' => 'string',
                'created' => 'datetime',
                'priority' => 'int',
            ],
        ];
    }

    static function add($q, $name, $priority, $created, $comment, int $org, int $responsible, int $contact)
    {
        return $q->table('potentials')
            ->field('name',
                    'priority',
                    'responsible',
                    'created',
                    'comment',
                    'org',
                    'contact',
                    'active')
                ->insert($name,
                         $priority,
                         $responsible,
                         $created,
                         $comment,
                         $org,
                         $contact,
                         true);
    }

    static function profile($q, int $org, int $id)
    {
        return $q->table('potentials', $id)
            ->where(['org', $org])
            ->field('id',
                    'name',
                    'priority',
                    'responsible',
                    'created',
                    'contact',
                    'comment',
                    'org',
                    'active')
            ->select(0);
    }

    static function list_by_active($q, int $org, int $page, bool $active)
    {
        return $q->table('potentials')
            ->where(['org', $org])
            ->where(['active', $active])
            ->order('>priority')
            ->field('id', 'name', 'priority', 'responsible.name responsible', 'contact')
            ->select(10, $page);
    }

    static function list_by_contact($q, int $org, int $contact)
    {
        return $q->table('potentials')
            ->where(['org', $org])
            ->where(['contact', $contact])
            ->field('id', 'name', 'priority', 'responsible.name responsible', 'active')
            ->select();
    }

    static function active($q, int $org, int $id, bool $active, int $responsible)
    {
        return $q->table('potentials', $id)
            ->where(['org', $org])
            ->field('active', 'responsible')
            ->update($active, $responsible);
    }

    static function edit($q, $id, $name, $priority, $comment, int $responsible, int $org)
    {
        return $q->table('potentials', $id)
            ->where(['org', $org])
            ->field('name', 'priority', 'comment', 'responsible')
            ->update($name, $priority, $comment, $responsible);
    }

    static function list_my_potential($q, int $org, int $responsible)
    {
        return $q->table('potentials')
            ->where(['org', $org])
            ->where(['responsible', $responsible])
            ->field('id',
                    'name',
                    'priority',
                    'contact.name contact-name',
                    'contact.phone1 contact-phone',
                    'contact.gender contact-gender',
                    'active')
            ->select(10);
    }
}

